package com.squid.templateProject.service.organization;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.squid.templateProject.bean.entity.OrganizationEntity;
import com.squid.templateProject.bean.enums.common.ActiveTokenEnums;
import com.squid.templateProject.bean.enums.common.DeleteTokenEnums;
import com.squid.templateProject.bean.enums.error.OrganizationError2000Enums;
import com.squid.templateProject.bean.req.PageReq;
import com.squid.templateProject.bean.req.organization.OrganizationInsertUpdateReq;
import com.squid.templateProject.bean.req.organization.OrganizationSelectPageReq;
import com.squid.templateProject.bean.resp.PageResp;
import com.squid.templateProject.bean.resp.organization.OrganizationSelectPageResp;
import com.squid.templateProject.config.exception.ExceptionFactory;
import com.squid.templateProject.converter.organization.OrganizationConverter;
import com.squid.templateProject.mapper.user.OrganizationEntityMapper;
import com.squid.templateProject.utils.LimitUtils;
import com.squid.templateProject.utils.TrimStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author jinhang1
 * @description 针对表【organization(组织表)】的数据库操作Service实现
 * @createDate 2024-01-23 17:28:39
 */
@Service
@Slf4j
public class OrganizationEntityServiceImpl extends ServiceImpl<OrganizationEntityMapper, OrganizationEntity>
        implements OrganizationEntityService {
    @Resource
    OrganizationEntityMapper organizationEntityMapper;

    @Override
    public void insertOrganization(OrganizationInsertUpdateReq req) {
        TrimStringUtils.trim(req);
        OrganizationEntity organizationEntity = OrganizationConverter.INSTANCE.to(req);
        repeatCheck(organizationEntity);
        try {
            this.save(organizationEntity);
        } catch (Exception e) {
            log.error("组织新增失败", e);
            throw ExceptionFactory.instanceBusinessException(OrganizationError2000Enums.Code_2001);
        }
    }

    private void repeatCheck(OrganizationEntity organizationEntity) {
        OrganizationEntity dataOrganization = this.getOne(new LambdaQueryWrapper<OrganizationEntity>()
                .eq(OrganizationEntity::getName, organizationEntity.getName())
                .eq(OrganizationEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                .eq(OrganizationEntity::getActiveToken, ActiveTokenEnums.Y.getCode())
                .ne(!ObjectUtils.isEmpty(organizationEntity.getId()), OrganizationEntity::getId, organizationEntity.getId())
        );
        if (!ObjectUtils.isEmpty(dataOrganization)) {
            log.error("当前组织已经存在:{}", organizationEntity.getName());
            throw ExceptionFactory.instanceBusinessException(OrganizationError2000Enums.Code_2000.getCode()
                    , StrUtil.format(OrganizationError2000Enums.Code_2000.getMessage()
                            , organizationEntity.getName()));
        }
    }

    @Override
    public void updateOrganization(OrganizationInsertUpdateReq req) {
        TrimStringUtils.trim(req);
        OrganizationEntity organizationEntity = OrganizationConverter.INSTANCE.to(req);
        repeatCheck(organizationEntity);
        try {
            this.updateById(organizationEntity);
        } catch (Exception e) {
            log.error("组织更新失败", e);
            throw ExceptionFactory.instanceBusinessException(OrganizationError2000Enums.Code_2002);
        }
    }

    @Override
    public void deleteOrganization(String id) {
        try {
            this.removeById(Long.valueOf(id));
        } catch (Exception e) {
            log.error("组织删除失败", e);
            throw ExceptionFactory.instanceBusinessException(OrganizationError2000Enums.Code_2003);
        }
    }

    @Override
    public PageResp<OrganizationSelectPageResp> pageOrganization(PageReq<OrganizationSelectPageReq> req) {
        Page<OrganizationSelectPageResp> page = new Page<>(req.getPageNum(), req.getPageSize());
        try {
            IPage<OrganizationSelectPageResp> response = organizationEntityMapper.page(page, req.getRequest());
            return LimitUtils.formatResponse(response);
        } catch (Exception e) {
            log.error("组织查询失败", e);
            throw ExceptionFactory.instanceBusinessException(OrganizationError2000Enums.Code_2004);
        }
    }
}




